home *** CD-ROM | disk | FTP | other *** search
- Program ToolDemo;
-
- { Turbo Pascal format }
-
- {$U-}
- {$R ToolDemoRes}
-
- Uses
- MemTypes,QuickDraw,OSIntf,ToolIntf;
-
- Const
- AppleMenu = 128;
- FileMenu = 129;
- EditMenu = 130;
- ToolMenu = 131;
- SprayItem = 1;
- BucketItem = 2;
- PatternMenu = 132;
- WhiteItem = 1;
- LtGrayItem = 2;
- GrayItem = 3;
- DkGrayItem = 4;
- BlackItem = 5;
-
- Var
- myMenus : array [AppleMenu..PatternMenu] of MenuHandle;
- myWindow : WindowPtr;
- Finished : Boolean;
- GrowArea : Rect;
- CurrentPat : Integer;
- CurrentTool : Integer;
-
- {###############################################################################}
-
- Function NewBitMap (VAR theBitMap : BitMap; theRect : Rect) : Ptr;
- Begin
- NewBitMap:= nil;
- with theBitMap,theRect do begin
- rowBytes:= ((right-left+15) DIV 16) *2;
- baseAddr:= NewPtr(rowBytes * (bottom-top));
- bounds:= theRect;
- if MemError = noErr then
- NewBitMap:= baseAddr;
- end;
- End;
-
- Procedure DoSprayCan (where : Point);
- Var
- workPort : GrafPtr;
- workBits : BitMap;
- workRect : Rect;
- workPat : Pattern;
- theStr : Str255;
- tempRect : Rect;
- SprayBits : BitMap;
- PatBits : BitMap;
- tickValue : LongInt;
- Begin
- GetPort(workPort);
- workBits:= workPort^.portBits;
- workRect:= workPort^.portRect;
- workPat:= workPort^.pnPat;
-
- GetIndString(theStr,128,1);
- SetRect(tempRect,0,0,16,16);
- if NewBitMap(SprayBits,tempRect) = nil then Exit;
- StuffHex(SprayBits.baseAddr,theStr);
-
- if NewBitMap(PatBits,workRect) = nil then begin
- DisposPtr(SprayBits.baseAddr);
- Exit;
- end;
- SetPortBits(PatBits);
- FillRect(workRect,workPat);
- SetPortBits(workBits);
-
- repeat
- GetMouse(where);
- with where do
- SetRect(tempRect,h-8,v-8,h+8,v+8);
- tickValue:= TickCount + 1;
- repeat until (tickValue <= TickCount);
- CopyMask(PatBits,SprayBits,workBits,tempRect,SprayBits.bounds,tempRect);
- tickValue:= TickCount;
- repeat until (tickValue <= TickCount);
- until NOT Button;
-
- DisposPtr(PatBits.baseAddr);
- DisposPtr(SprayBits.baseAddr);
- End;
-
- Procedure DoPaintBucket (where : Point);
- Var
- workPort : GrafPtr;
- workBits : BitMap;
- workRect : Rect;
- workPat : Pattern;
- PatBits : BitMap;
- onBlack : Boolean;
- srcMap : BitMap;
- dstMap : BitMap;
- srcPtr : Ptr;
- dstPtr : Ptr;
- srcRow : Integer;
- dstRow : Integer;
- height : Integer;
- words : Integer;
- Begin
- GetPort(workPort);
- workBits:= workPort^.portBits;
- workRect:= workPort^.portRect;
- workPat:= workPort^.pnPat;
-
- if NewBitMap(dstMap,workRect) = nil then Exit;
- if NewBitMap(srcMap,workRect) = nil then begin
- DisposPtr(dstMap.baseAddr);
- Exit;
- end;
-
- CopyBits(workBits,srcMap,workRect,workRect,srcCopy,nil);
-
- onBlack:= GetPixel(where.h,where.v);
- if onBlack then begin
- SetPortBits(srcMap);
- InvertRect(workRect);
- SetPortBits(workBits);
- end;
-
- if NewBitMap(PatBits,workRect) = nil then begin
- DisposPtr(dstMap.baseAddr);
- DisposPtr(srcMap.baseAddr);
- Exit;
- end;
- SetPortBits(PatBits);
- FillRect(workRect,workPat);
- if onBlack then InvertRect(workRect);
- SetPortBits(workBits);
-
- srcPtr:= srcMap.baseAddr;
- srcRow:= srcMap.rowBytes;
-
- dstPtr:= dstMap.baseAddr;
- dstRow:= dstMap.rowBytes;
-
- height:= dstMap.bounds.bottom - dstMap.bounds.top;
- words:= (dstRow + 1) DIV 2;
-
- SeedFill(srcPtr,dstPtr,srcRow,dstRow,height,words,where.h,where.v);
- CopyMask(PatBits,dstMap,srcMap,workRect,workRect,srcMap.bounds);
-
- if onBlack then begin
- SetPortBits(srcMap);
- InvertRect(workRect);
- SetPortBits(workBits);
- end;
- CopyBits(srcMap,workBits,workRect,workRect,srcCopy,nil);
-
- DisposPtr(srcMap.baseAddr);
- DisposPtr(dstMap.baseAddr);
- DisposPtr(PatBits.baseAddr);
- End;
-
- {###############################################################################}
-
- Procedure ProcessMenu (codeWord : LongInt);
- Var
- menuNum : Integer;
- itemNum : Integer;
- itemStr : Str255;
- dummy : Integer;
- Begin
- if codeWord <> 0 then begin
- menuNum := HiWord(codeWord);
- itemNum := LoWord(codeWord);
- case menuNum of
- AppleMenu :
- begin
- GetItem(myMenus[AppleMenu],itemNum,itemStr);
- dummy:= OpenDeskAcc(itemStr);
- end;
- FileMenu : Finished:= TRUE;
- EditMenu : if NOT SystemEdit(itemNum - 1) then ;
- ToolMenu :
- begin
- CheckItem(myMenus[ToolMenu],CurrentTool,false);
- CurrentTool:= itemNum;
- CheckItem(myMenus[ToolMenu],CurrentTool,true);
- end;
- PatternMenu :
- begin
- CheckItem(myMenus[PatternMenu],CurrentPat,false);
- CurrentPat:= itemNum;
- CheckItem(myMenus[PatternMenu],CurrentPat,true);
- SetPort(myWindow);
- case CurrentPat of
- WhiteItem : PenPat(white);
- LtGrayItem : PenPat(ltGray);
- GrayItem : PenPat(gray);
- DkGrayItem : PenPat(dkGray);
- BlackItem : PenPat(black);
- end
- end;
- end; {case}
- HiliteMenu(0);
- end; {big if}
- End;
-
- {###############################################################################}
-
- Procedure DealWithMouseDowns(theEvent: EventRecord);
- Var
- whichWindow : WindowPtr;
- mouseLoc : Point;
- windowLoc : Integer;
- position : LongInt;
- Begin
- mouseLoc:= theEvent.where;
- windowLoc:= FindWindow(mouseLoc,whichWindow);
- case windowLoc of
- inMenuBar : ProcessMenu(MenuSelect(mouseLoc));
- inSysWindow : SystemClick(theEvent,whichWindow);
- inDrag : DragWindow(whichWindow,mouseLoc,screenBits.bounds);
- inGoAway : if TrackGoAway(whichWindow,mouseLoc) then Finished:= true;
- inGrow :
- begin
- position:= GrowWindow(whichWindow,mouseLoc,GrowArea);
- if position <> 0 then begin
- SizeWindow(whichWindow,loword(position),hiword(position),false);
- SetPort(whichWindow);
- InvalRect(whichWindow^.portRect);
- end;
- end;
- inZoomIn,inZoomOut :
- begin
- if TrackBox(whichWindow,mouseLoc,windowLoc) then begin
- SetPort(whichWindow);
- ClipRect(whichWindow^.portRect);
- EraseRect(whichWindow^.portRect);
- ZoomWindow(whichWindow,windowLoc,true);
- InvalRect(whichWindow^.portRect);
- end;
- end;
- inContent :
- begin
- if whichWindow <> FrontWindow then
- SelectWindow(whichWindow)
- else begin
- SetPort(whichWindow);
- GlobalToLocal(mouseLoc);
- case CurrentTool of
- SprayItem : DoSprayCan(mouseLoc);
- BucketItem : DoPaintBucket(mouseLoc);
- end;
- end;
- end;
- end;
- End;
-
- Procedure DealWithKeyDowns(theEvent: EventRecord);
- Var
- CharCode : char;
- Begin
- CharCode:= CHR(BitAnd(theEvent.message,charCodeMask));
- if BitAnd(theEvent.modifiers,CmdKey) = CmdKey
- then ProcessMenu(MenuKey(CharCode));
- End;
-
- Procedure DealWithActivates(theEvent: EventRecord);
- Var
- theWindow : WindowPtr;
- Begin
- theWindow := WindowPtr(theEvent.message);
- if Odd(theEvent.modifiers)
- then SetPort(theWindow);
- End;
-
- Procedure DealWithUpdates(theEvent: EventRecord);
- Var
- theWindow : WindowPtr;
- tempPort : WindowPtr;
- Begin
- theWindow := WindowPtr(theEvent.message);
- GetPort(tempPort);
- SetPort(theWindow);
- BeginUpDate(theWindow);
- ClipRect(theWindow^.portRect);
- EraseRect(theWindow^.portRect);
- PenSize(5,5);
- FrameOval(theWindow^.portRect);
- PenSize(1,1);
- EndUpDate(theWindow);
- SetPort(tempPort);
- End;
-
- Procedure MainEventLoop;
- Var
- Event : EventRecord;
- Begin
- repeat
- SystemTask;
- if GetNextEvent(everyEvent, Event) then
- case Event.what of
- mouseDown : DealWithMouseDowns(Event);
- AutoKey : DealWithKeyDowns(Event);
- KeyDown : DealWithKeyDowns(Event);
- ActivateEvt : DealWithActivates(Event);
- UpdateEvt : DealWithUpdates(Event);
- end; {case}
- until Finished;
- End;
-
- {###############################################################################}
-
- Procedure SetupStuff;
- Var
- index : Integer;
- Begin
- MaxApplZone;
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(nil);
-
- for index:= AppleMenu to PatternMenu do begin
- myMenus[index] := GetMenu(index);
- InsertMenu(myMenus[index],0);
- end;
- AddResMenu(myMenus[AppleMenu],'DRVR');
- DrawMenuBar;
-
- CurrentTool:= SprayItem;
- CurrentPat:= BlackItem;
-
- CheckItem(myMenus[ToolMenu],CurrentTool,true);
- CheckItem(myMenus[PatternMenu],CurrentPat,true);
-
- myWindow:= GetNewWindow(1000,nil,pointer(-1));
-
- Finished:= false;
- with screenBits.bounds do
- SetRect(GrowArea,150,150,right,bottom);
-
- FlushEvents(everyEvent,0);
-
- InitCursor;
- End;
-
- Begin
- SetupStuff;
-
- MainEventLoop;
- End.
-